<?php

require_once("DB.php");

new Controller();

class Controller {
    
    public function __construct() {
        
        if (filter_has_var(INPUT_POST, 'option'))
        {
            switch (filter_input(INPUT_POST, 'option'))
            {
                case 'getServices':
                    echo $this->getServices();
                    break;
                case 'deleteService':
                    echo $this->deleteService();
                    break;
                case 'getInfoService':
                    echo $this->getInfoService();
                    break;    
                case 'insertService':
                    echo $this->insertService();
                    break;                
                case 'updateService':
                    echo $this->updateService();
                    break;
            }
        }
                    
    }

    public function getServices(){
        
        $db = new DB();
        
        $data = array();
        $sql = <<<SQL
                select service_id, 
                       name, 
                       date_format(start_date_active, '%d/%m/%Y') start_date_active,
                       date_format(end_date_active, '%d/%m/%Y') end_date_active, 
                       status 
                  from pa_services
                 order by name
SQL;
        
        if ($db->Query($sql)) {
            while ($row = $db->Next()) {
                $data[] = $row;
            }
        } else {
            $data = array("success" => 0, "message" => 'Error en ejecucion de Query', 'sql' => $sql);
        }
        return json_encode($data);       
        
    }
    
    public function getInfoService(){
        
        $serviceID = filter_input(INPUT_POST, 'serviceID');
        
        $db = new DB();
        
        $data = array();
        $sql = <<<SQL
                select name, 
                       date_format(start_date_active, '%d/%m/%Y') start_date_active,
                       date_format(end_date_active, '%d/%m/%Y') end_date_active, 
                       status 
                  from pa_services
                 where service_id = %service_id
SQL;
        
        $sql = str_replace("%service_id", $serviceID, $sql);
        
        if ($db->Query($sql)) {
            while ($row = $db->Next()) {
                $data = $row;
            }
        } else {
            $data = array("success" => 0, "message" => 'Error en ejecucion de Query', 'sql' => $sql);
        }
        return json_encode($data);
        
    }    
    
    public function deleteService(){
        
        $serviceID = filter_input(INPUT_POST, 'serviceID');
        
        $db = new DB();
        
        $data = array();
        $sql = "delete from pa_services where service_id = %s";
        
        $sql = sprintf($sql, $serviceID);
        
        if ($db->Query($sql))
            $data = array("success" => 1, "message" => 'Servicio eliminado correctamente.');
        else
            $data = array("success" => 0, "message" => 'Error en ejecucion de Query', 'sql' => $sql);
        
        return json_encode($data);       
        
    }    
    
    public function insertService(){
        
        $name = filter_input(INPUT_POST, 'name');
        $start_date_active = filter_input(INPUT_POST, 'start_date_active');
        $end_date_active = filter_input(INPUT_POST, 'end_date_active');
        $status = filter_input(INPUT_POST, 'status');
        
        $start_date_active_1 =         substr($start_date_active, 6, 4) 
                               . "-" . substr($start_date_active, 3, 2)
                               . "-" . substr($start_date_active, 0, 2);
                               
        $end_date_active_1 =         substr($end_date_active, 6, 4) 
                             . "-" . substr($end_date_active, 3, 2)
                             . "-" . substr($end_date_active, 0, 2);                               
        
        if ($status == "")
            $status = "I";
        else
            $status = "A";
        
        $db = new DB();
        
        $data = array();
        $sql = <<<SQL
                 insert into pa_services(name, 
                                         start_date_active, 
                                         end_date_active, 
                                         status)
                                  values('%name%',
                                         '%start_date_active%',
                                         '%end_date_active%',
                                         '%status%')
SQL;
        
        $sql = str_replace("%name%", $name, $sql);
        $sql = str_replace("%start_date_active%", $start_date_active_1, $sql);
        $sql = str_replace("%end_date_active%", $end_date_active_1, $sql);
        $sql = str_replace("%status%", $status, $sql);
        
        if ($db->Query($sql)){
            
            $sql = "select last_insert_id() last_id";
            if ($db->Query($sql)) {
                while ($row = $db->Next())
                    $data = $row;
                
                $sql = <<<SQL
                         select max(service_id) service_id
                           from pa_services
                          where name = ( select max(name) 
                                           from pa_services
                                          where name < '%name%' )
SQL;
                $sql = str_replace("%name%", $name, $sql);
                if ($db->Query($sql)){
                    $row = $db->Next();
                    $data['afterServiceID'] = $row['service_id'];
                }
                else
                    $data = array("success" => 0, "message" => 'Error recuperando el serviceAfter', 'sql' => $sql);
            }
            else
                $data = array("success" => 0, "message" => 'Error recuperando el ultimo ID para el servicio', 'sql' => $sql);
        }        
        else
            $data = array("success" => 0, "message" => 'Error insertando el servicio', 'sql' => $sql);
            
        return json_encode($data);
        
    }
    
    public function updateService(){
        
        $name = filter_input(INPUT_POST, 'name');
        $start_date_active = filter_input(INPUT_POST, 'start_date_active');
        $end_date_active = filter_input(INPUT_POST, 'end_date_active');
        $status = filter_input(INPUT_POST, 'status');
        $service_id = filter_input(INPUT_POST, 'serviceID');
        
        $start_date_active_1 =         substr($start_date_active, 6, 4) 
                               . "-" . substr($start_date_active, 3, 2)
                               . "-" . substr($start_date_active, 0, 2);
                               
        $end_date_active_1 =         substr($end_date_active, 6, 4) 
                             . "-" . substr($end_date_active, 3, 2)
                             . "-" . substr($end_date_active, 0, 2);                               
        
        if ($status == "")
            $status = "I";
        else
            $status = "A";
        
        $db = new DB();
        
        $data = array();
        $sql = <<<SQL
                update pa_services
                   set name              = '%name%',
                       start_date_active = '%start_date_active%',
                       end_date_active   = '%end_date_active%',
                       status            = '%status%'
                 where service_id = %service_id%
SQL;
        
        $sql = str_replace("%name%", $name, $sql);
        $sql = str_replace("%start_date_active%", $start_date_active_1, $sql);
        $sql = str_replace("%end_date_active%", $end_date_active_1, $sql);
        $sql = str_replace("%status%", $status, $sql);
        $sql = str_replace("%service_id%", $service_id, $sql);
        
        if ($db->Query($sql))
            $data = array("success" => 1, "message" => 'Servicio actualizado correctamente.');
        else
            $data = array("success" => 0, "message" => 'Error en ejecucion de Query', 'sql' => $sql);
        
        return json_encode($data);       
        
    }        
    
}